package com.loongxun.playwithyou.ui.activity.user;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.loongxun.playwithyou.ApiInterface;
import com.loongxun.playwithyou.R;
import com.loongxun.playwithyou.bean.SendRequest;
import com.loongxun.playwithyou.network.MyHttpClientRespenseHandler;
import com.loongxun.playwithyou.network.nHttpClient;
import com.loongxun.playwithyou.util.ReflectionUtils;
import com.loongxun.playwithyou.util.ToastView;

import org.json.JSONObject;

import cz.msebera.android.httpclient.util.TextUtils;

/**
 * 修改密码UI
 * Created by Administrator on 2017/8/19.
 */
public class PasswordChangeActivity extends Activity implements View.OnClickListener{

    private Context context;
    //用户编辑器
    private SharedPreferences share;
    private SharedPreferences.Editor editor;
    private SendRequest send;
    private String user_id,password,pass;

    //标题
    private TextView tv_top_title;
    private LinearLayout ll_ch_ps_old_pass;
    private EditText et_ps_old_pass,et_ps_new_pass,et_ps_confirm_pass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_password_change);
        context = PasswordChangeActivity.this;

        share = context.getSharedPreferences("userInfo", 0);
        editor = share.edit();
        user_id = share.getString("user_id", "");
        password = share.getString("password", "");

        initView();
    }

    private void initView() {
        findViewById(R.id.iv_back).setOnClickListener(this);
        tv_top_title = (TextView) findViewById(R.id.tv_top_title);
        tv_top_title.setText("修改密码");

        ll_ch_ps_old_pass = (LinearLayout) findViewById(R.id.ll_ch_ps_old_pass);
        et_ps_old_pass = (EditText) findViewById(R.id.et_ps_old_pass);
        et_ps_new_pass = (EditText) findViewById(R.id.et_ps_new_pass);
        et_ps_confirm_pass = (EditText) findViewById(R.id.et_ps_confirm_pass);

        findViewById(R.id.tv_ps_confirm).setOnClickListener(this);

        if ("".equals(password)) {
            ll_ch_ps_old_pass.setVisibility(View.GONE);
        }else {
            ll_ch_ps_old_pass.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onClick(View view) {
        ToastView toast;
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_ps_confirm:
                String oldPass = et_ps_old_pass.getText().toString().trim();
                String newPass = et_ps_new_pass.getText().toString().trim();
                String conPass = et_ps_confirm_pass.getText().toString().trim();
                if (!"".equals(password) && TextUtils.isEmpty(oldPass)) {
                    toast = new ToastView(context, "请输入旧密码");
                    toast.show();
                } else if (!"".equals(password) && !ReflectionUtils.isPassword(oldPass)) {
                    toast = new ToastView(context, "密码格式错误，请输入长度8-16位数字和字母组合");
                    toast.show();
                }else if (TextUtils.isEmpty(newPass)){
                    toast = new ToastView(context, "请输入新密码");
                    toast.show();
                }else if (!ReflectionUtils.isPassword(newPass)) {
                    toast = new ToastView(context, "密码格式错误，请输入长度8-16位数字和字母组合");
                    toast.show();
                }else if (oldPass.equals(newPass)) {
                    toast = new ToastView(context, "新旧密码一致，请重新输入");
                    toast.show();
                }else if (TextUtils.isEmpty(conPass)){
                    toast = new ToastView(context, "请输入确认密码");
                    toast.show();
                }else if (!newPass.equals(conPass)) {
                    toast = new ToastView(context, "密码不一致，请重新输入");
                    toast.show();
                }else {
                    try {
                        send = new SendRequest();
                        send.userId = user_id;
                        send.oldPassword = oldPass;
                        send.newPassword = newPass;
                        send.conNewPassword = conPass;
                        pass = conPass;
                        //注册
                        nHttpClient.getAll(ApiInterface.UP_PASS, send.updatePassJson().toString(), updatePassHandler);
                    }catch (Exception e){
                        toast = new ToastView(context, "参数错误");
                        toast.show();
                    }
                }
                break;
        }
    }

    private MyHttpClientRespenseHandler updatePassHandler = new MyHttpClientRespenseHandler() {

        @Override
        public void success(JSONObject json) {
            Integer succeed = json.optJSONObject("status").optInt("succeed", 0);
            if (succeed == 1) {
                new ToastView(context, "密码修改成功").show();
                editor.putString("password", pass);
                editor.commit();//提交修改
                CloseKeyBoard();
                finish();
            }else {
                String error_desc = json.optJSONObject("status").optString("error_desc");
                new ToastView(context, error_desc).show();
            }
        }

        @Override
        public void faile(String json) {
            new ToastView(context, json).show();
        }
    };

    // 关闭键盘
    public void CloseKeyBoard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(et_ps_confirm_pass.getWindowToken(), 0);
    }
}
